import {
  exportUrl,
  getSimulatedTypeList,
  downloadTemplateUrl,
  getOrderStatusList,
  getSalesOrderList,
  getProdOrderTypeList,
  getLineCodeListNew,
} from "@/api/order/mesProdOrderPlan";
import {
  getBomVersionList
} from "@/api/order/mesProdOrder";
import { getUnitDictList } from "@/api/common";

import excelUpload from "@/components/excel-upload/excelUpload";
import { checkStr } from "@/utils/judge";
import url from "@/axios/url";

const { mesUrl } = url();
let vm = null;
const sendThis = (_this) => {
  vm = _this;
};
export { sendThis };

// 列表头按钮配置
export const operateList = [
  {
    name: $i18n.t("common.export"),
    type: "primary",
    icon: "el-icon-download",
    handleName: "export", //点击时返回的标识
    url: exportUrl,
    //auth: "auth:operlog:export", //权限字段
  },
  {
    name: $i18n.t("common.add"),
    type: "primary",
    icon: "el-icon-circle-plus-outline",
    handleName: "add", //点击时返回的标识
  },
  {
    name: $i18n.t("common.delete"),
    type: "danger",
    icon: "el-icon-delete",
    handleName: "deleteBatch", //点击时返回的标识
    isMultiple: true, // isSingle 表示必须选择一项//isMultiple: true, // 表示必须多选
    // disabled: (ite) => {
    //   return ite.length != 1 ? true : false;
    // },
  },
  {
    // 导入文件
    render: (h, scope) => {
      // scope列表上选择的数据
      const uploadUrl = `${mesUrl}mesProdOrderPlan/importData`;
      return (
        <excelUpload
          action={uploadUrl}
          on={{
            handleUploadError: (err) => {
              // 失败的回调
              console.log(err);
              vm.refresh();
            },
            handleUploadSuccess: (res) => {
              // 成功 的回调
              console.log(res);
              vm.refresh();
            },
          }}
        />
      );
    },
  },
  {
    name: $i18n.t("mesProdOrderPlan.schedule"),
    type: "primary",
    icon: "el-icon-circle-plus-outline",
    handleName: "addOrder", //点击时返回的标识
    disabled: (data) => {
      return data.length != 1 || data[0].orderStatus == 2;
    },
  },
  {
    name: $i18n.t("common.downloadTemplate"),
    type: "success",
    icon: "el-icon-download",
    handleName: "exportExcel", // 点击时返回的标识
  },
];
// 行末按钮配置
export const rowConfig = [
  {
    name: $i18n.t("common.edit"),
    type: "primary",
    class: "row-btn-primary",
    handleName: "edit", //点击时返回的标识
    show: (row) => {
      return row.orderStatus == '-1';
    },
  },
  {
    name: $i18n.t("common.details"),
    type: "info",
    class: "row-btn-info",
    handleName: "detail", //点击时返回的标识
  },
  {
    name: $i18n.t("common.delete"),
    type: "danger",
    class: "row-btn-danger",
    handleName: "delete", //点击时返回的标识
  },
];
// 搜索条件配置
export const searchConfig = {
  data() {
    const self = this;
    return {
      searchDialog: {
        inline: true,
        formDesc: {
          //    saleCode: {
          //      type: "input",
          //      label: "销售订单编码",
          //    },
          planCode: {
            type: "input",
            label: $i18n.t("mesProdOrderPlan.productionOrderCode"),
          },
          orderStatus: {
            label: $i18n.t("mesProdOrderPlan.orderStatus"),
            type: "select",

            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getOrderStatusList();
              return list.data.result;
            },
          },
          prodCode: {
            type: "input",
            label: $i18n.t("mesProdOrderPlan.prodCode"),
          },
          prodName: {
            type: "input",
            label: $i18n.t("mesProdOrderPlan.prodName"),
          },
          planBeginDate: {
            type: "daterange",

            label: $i18n.t("mesProdOrderPlan.planBeginDate"),
            attrs: {
              valueFormat: "yyyy-MM-dd",
              searchArrKey: ["planBeginDateMin", "planBeginDateMax"],
            },
          },
          workshopCode: {
            label: $i18n.t("mesProdOrderPlan.workshopName"),
            type: "select",
            prop: {
              value: "modelingCode",
              text: "modelingName",
            },
            options: async (data) => {
              const params = {
                modelingCode: self.$store.getters.factoryCode,
                level: 4,
                modelingType: "0",
              };
              const list = await getLineCodeListNew(params);
              return list.data.result;
            },
          },
        },
      },
    };
  },
};
// 新增/删除表单配置
export const formDescDialogMixin = {
  data() {
    const self = this;
    var validatePass = (rule, value, callback) => {
      if (!checkStr(value, "english-number-")) {
        callback(new Error($i18n.t("mesProdOrderPlan.rule11")));
      } else {
        callback();
      }
    };
    return {
      formDescDialog: {
        span: 24,
        disabled: false,
        isShowSubmitBtn: true,
        labelPosition: "right",
        rules: {
          planCode: [
            {
              validator: validatePass,
              trigger: ["change", "blur"],
            },
          ],
        },
        formDesc: {
          planCode: {
            type: "input",
            label: $i18n.t("mesProdOrderPlan.productionOrderCode"),
            layout: 8,
            disabled: (data) => {
              return !!data.codeStrategy || !!data.id;
            },
            required: true,
          },
          prodCode: {
            type: "select",
            label: $i18n.t("mesProdOrderPlan.goodsCode"),
            layout: 8,
            required: true,
          },
          prodName: {
            type: "input",
            label: $i18n.t("mesProdOrderPlan.goodsName"),
            layout: 8,
            required: false,
            disabled: true,
          },
          seriesCode: {
            type: "input",
            label: $i18n.t("mesProdOrderPlan.seriesCode"),
            layout: 8,
            required: true,
            disabled: true,
          },
          seriesName: {
            type: "input",
            label: $i18n.t("mesProdOrderPlan.seriesName"),
            layout: 8,
            required: false,
            disabled: true,
          },
          unitCode: {
            type: "select",
            label: $i18n.t("mesProdOrderPlan.measureUnit2"),
            layout: 8,
            required: true,
            prop: {
              value: "value",
              text: "description",
            },
            options: async () => {
              const list = await getUnitDictList();
              self.getDictList = list.data.result;
              return list.data.result;
            },
            on: {
              change: (val) => {
                self.handelSelectEqu(val)
              }
            }
          },
          // unit: {
          //   type: "input",
          //   label: $i18n.t("mesProdOrderPlan.measureUnit2Code"),
          //   layout: 8,
          //   required: false,
          //   disabled: true,
          // },
          prodOrderType: {
            type: "select",
            label: $i18n.t("mesProdOrderPlan.prodOrderType"),
            layout: 8,
            required: true,
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getProdOrderTypeList();
              return list.data.result;
            },
          },
          orderStatus: {
            type: "select",
            label: $i18n.t("mesProdOrderPlan.orderStatus"),
            layout: 8,
            required: false,
            disabled: true,
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getOrderStatusList();
              return list.data.result;
            },
          },
          quantity: {
            type: "number",
            label: $i18n.t("mesProdOrderPlan.planQuantity"),
            layout: 8,
            required: true,
            attrs: {
              min: 0,
              precision: 1,
              step: 0.1,
            },
          },
          actualQuantity: {
            type: "number",
            label: $i18n.t("mesProdOrderPlan.actualQuantity"),
            layout: 8,
            required: false,
            disabled: true,
          },

          planBeginDate: {
            type: "date",
            label: $i18n.t("mesProdOrderPlan.planBeginDate"),
            layout: 8,
            required: true,
            attrs: {
              valueFormat: "yyyy-MM-dd",
            },
            on: {
              change: this.handleStartDate,
            },
          },
          planEndDate: {
            type: "date",
            label: $i18n.t("mesProdOrderPlan.planEndDate"),
            layout: 8,
            required: true,
            attrs: {
              valueFormat: "yyyy-MM-dd",
            },
            on: {
              change: this.handleEndDate,
            },
          },
          actualBeginDate: {
            type: "date",
            label: $i18n.t("mesProdOrderPlan.actualBeginDate"),
            layout: 8,
            required: false,
            disabled: true,
            attrs: {
              valueFormat: "yyyy-MM-dd",
            },
          },
          actualEndDate: {
            type: "date",
            label: $i18n.t("mesProdOrderPlan.actualEndDate"),
            layout: 8,
            required: false,
            disabled: true,
            attrs: {
              valueFormat: "yyyy-MM-dd",
            },
          },

          bomVersionNumber: {
            type: "select",
            label: $i18n.t("mesProdOrderPlan.bomVersionNumber"),
            layout: 8,
            required: false,
            disabled: false,
            prop: {
              value: "bomVersion",
              text: "bomVersion",
            },
            optionsLinkageFields: ["prodCode"],
            options: async (data) => {
              if (data && data.prodCode) {
                const materialCode = data.prodCode
                const list = await getBomVersionList({ "materialCode": materialCode, bomStatus: 1 });
                return list.data.result;
              } else {
                return []
              }
            },
          },
          workshopCode: {
            type: "select",
            label: $i18n.t("mesProdOrderPlan.workshopName"),
            layout: 8,
            required: false,
            disabled: false,
            prop: {
              value: "modelingCode",
              text: "modelingName",
            },
            options: async (data) => {
              self.editWorkshopList = []
              const list = await getLineCodeListNew({
                modelingCode: this.$store.getters.factoryCode,
                level: 4,
                modelingType: '0'
              })
              self.editWorkshopList = list.data.result
              return list.data.result
            },
            on: {
              change: (val) => {
                self.handelWorkshop(val)
              }
            }
          },
          lineCode: {
            type: "select",
            label: $i18n.t("mesProdOrderPlan.lineName"),
            layout: 8,
            required: false,
            disabled: false,
            prop: {
              value: "modelingCode",
              text: "modelingName",
            },
            optionsLinkageFields: ["workshopCode"],
            options: async (data) => {
              self.editLineList = []
              if (data && data.workshopCode) {
                const list = await getLineCodeListNew({
                  modelingCode: data.workshopCode,
                  level: 5,
                  modelingType: '0'
                })
                self.editLineList = list.data.result
                return list.data.result
              } else {
                return []
              }
            },
            on: {
              change: (val) => {
                self.handelLine(val)
              }
            }
          },

          remark: {
            type: "textarea",
            label: $i18n.t("common.remark"),
            layout: 24,
            required: false,
          },
        },
      },
    };
  },
  methods: {
    handelSelectEqu(val) {
      let description = (this.getDictList.find(i => i.value == val) || { description: '' }).description
      this.$set(this.formDataDialog, 'unit', description)
    },
    handelWorkshop(val) {
      let modelingName = (this.editWorkshopList.find(i => i.modelingCode == val) || { modelingName: '' }).modelingName
      this.$set(this.formDataDialog, 'workshopName', modelingName)
    },
    handelLine(val) {
      let modelingName = (this.editLineList.find(i => i.modelingCode == val) || { modelingName: '' }).modelingName
      this.$set(this.formDataDialog, 'lineName', modelingName)
    }
  },
};

//导入数据
export const importFormDescDialog = {
  span: 24,
  disabled: false,
  isShowSubmitBtn: true,
  formDesc: {
    model: {
      type: "button",
      label: "",// $i18n.t("mesProdOrderPlan.model"),
      layout: 24,
      required: false,
      title: $i18n.t('common.downloadTemplate'),
      style: {
        color: "#1867c0",
        fontSize: "14px",
      },
      on: {
        click: () => {
          exportExcel(downloadTemplateUrl);
        },
      },
    },
    file: {
      type: "input",
      label: $i18n.t("mesProdOrderPlan.file"),
      layout: 24,
      // required: true,
    },
  },
};
